//
//  CateTopViewController.m
//  DanTang
//
//  Created by honeyrr on 2017/9/28.
//  Copyright © 2017年 honeyrr. All rights reserved.
//

#import "CateTopViewController.h"
#import "CateTopCollectionViewCell.h"
#import "CateTableViewController.h"
#import "CateSeeAllTableViewControllTableViewController.h"

static NSString * const cateTopCollectionCell = @"cateTopCollectionCell";
@interface CateTopViewController ()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property(nonatomic, strong) NSArray *collections;
@property(nonatomic, strong) UICollectionView *collectionView;

@end

@implementation CateTopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self loadData];
    
}

-(NSArray *)collections
{
    if (!_collections) {
        _collections = [NSArray array];
    }
    return _collections;
}

-(void) loadData
{
    NSString *urlString = @"http://api.dantangapp.com/v1/collections?limit=20&offset=0";
    [[DTNetWork sharedNetWork] loadDataInfo:urlString parameters:nil success:^(id  _Nullable responseObject) {
        NSLog(@"res------:%@",responseObject);
        _collections = responseObject[@"data"][@"collections"];
        [self.collectionView reloadData];
    } failure:^(NSError * _Nullable error) {
        
    }];
    
    [self setUpUI];
}

-(void) setUpUI
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc]init];
    label.mr_x = 10;
    label.mr_y = 5;
    label.mr_size = CGSizeMake(100, 35);
    label.text = @"专题集合";
    label.font = [UIFont systemFontOfSize:16];
    label.textColor = [UIColor grayColor];
    [self.view addSubview:label];
    
    //查看全部
    UIButton *btn = [[UIButton alloc]init];
    [btn setTitle:@"查看全部>" forState:UIControlStateNormal];
    [btn sizeToFit];
    btn.mr_x = self.view.frame.size.width -  5 - label.mr_width;
    btn.mr_centerY = label.mr_centerY;
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [self.view addSubview:btn];
    [btn addTarget:self action:@selector(showAll:) forControlEvents:UIControlEventTouchUpInside];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(150, 80);
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //滑动
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 40, DTScreenW, 100) collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:collectionView];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([CateTopCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:cateTopCollectionCell];
    collectionView.showsHorizontalScrollIndicator = NO;
    _collectionView = collectionView;
}
-(void) showAll:(UIButton *)sender
{
    [self.navigationController pushViewController:[CateSeeAllTableViewControllTableViewController new] animated:YES];
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collections.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CateTopCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cateTopCollectionCell forIndexPath:indexPath];
    cell.collection = self.collections[indexPath.row];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    CateTableViewController *tvc = [CateTableViewController new];
    tvc.type = @"专题集合";
    tvc._id = [self.collections[indexPath.row][@"collectionID"] integerValue];
    tvc.title = self.collections[indexPath.row][@"title"];
    [self.navigationController pushViewController:tvc animated:YES];
}

@end
